// pages/goods_details/index.js
var app = getApp(); //引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var baseURL1 = app.data.baseURL;
var jielong = app.data.jielong;
var jlpay = app.data.jlpay;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434291.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047712.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    num:0,
    isshow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.setData({
      status:options.status
    })
    var xiangqing='';
    wx.getStorage({
      key: 'detail',
      success: function(res) {
        that.setData({
          detail:res.data
        })
        if (res.data.gdesc == "") {
          (xiangqing = "暂无详情信息")
        } else {
          xiangqing = res.data.gdesc;
          // console.log(xiangqing);
          // var arrimg=xiangqing.match(/(http:\/\/)[a-zA-Z0-9.\/]*(.jpg){0,1}/g);
          // var txt=xiangqing.match(/((&gt;)|(&lt;))[\u4e00-\u9fa5,，。？！“”""/;；、_-]{2,}((&gt;)|(&lt;))/g);
          //var txt=xiangqing.match(/((((&gt;)|(&lt;))[\u4e00-\u9fa5,，。？！“”""/;；、_-]{2,}((&gt;)|(&lt;))))|((&quot;))[\u4e00-\u9fa5,，。？！“”""/;；、_-]{2,}((&quot;))/g);
          // var arrtxt=[];
          // if(!!txt){
          //   for(var i=0;i<txt.length;i++){
          //     arrtxt[i]=txt[i].slice(4,-4);
          //   }
          // }
          that.setData({
            xiangqing: that.escapeHtml(xiangqing)
          })
          console.log(that.escapeHtml(xiangqing))
        }
      },
    })
    wx.getStorage({
      key: 'product',
      success: function(res) {
        that.setData({
          product: res.data
        })
      },
    })
    wx.getStorage({
      key: 'tzInfo',
      success: function (res) {
        that.setData({
          tzInfo: res.data
        })
      },
    });
  },
  gohome:function(){
    wx.navigateTo({
      url: '../Tindex/Tindex',
    })
  },
  hide:function(){
    this.setData({
      isshow:false
    })
  },
  toPlus:function(e){
    console.log(e)
    var that=this;
    wx.setStorage({
      key: 'goodsArr',
      data: [],
    })
    if (this.data.detail.one_limit == 0) {
      if (this.data.num > 0) {
        wx.showToast({
          title: '该商品单次限购1件',
          icon: 'none'
        })
        return;
      }
    } else {
      if (this.data.num + 1 > Number(this.data.detail.one_limit)) {
        wx.showToast({
          title: '该商品单次已限购' + this.data.detail.one_limit + '件',
          icon: 'none'
        })
        return;
      }
    }
    //总限购
    if (this.data.detail.many_limit == 0) {
      if (this.data.num > 999999) {
        wx.showToast({
          title: '该商品已达到总限购数量',
          icon: 'none'
        })
        return;
      }
    } else {
      console.log(this.data.num + 1 + Number(this.data.detail.historynum), Number(this.data.detail.many_limit))
      if (this.data.num + 1 + Number(this.data.detail.historynum) > Number(this.data.detail.many_limit)) {
        wx.showToast({
          title: '该商品已达到总限购数量' + this.data.detail.many_limit + '件',
          icon: 'none'
        })
        return;
      }
    }
    var num = e.currentTarget.dataset.num+1;
    that.setData({
      num:num
    })
  },
  toReduce:function(e){
    var that = this;
    if (e.currentTarget.dataset.num>0){
      var num = e.currentTarget.dataset.num - 1;
      that.setData({
        num: num
      })
    }else{
      wx.showToast({
        title: '不能再减啦',
        icon:'none'
      })
    } 
  },
  escapeHtml: function (text) {
    var a = text.replace(/(\&lt;)/g, "<");
    var b = a.replace(/(\&gt;)/g, ">");
    var c = b.replace(/(\&amp;)/g, "\&");
    var d = c.replace(/(\&quot;)/g, "\"");
    return d;
  },
  gobuy:function(){
    var that=this;
    if (this.data.detail.gnum == 0) {
      wx.showToast({
        title: '该商品已售完',
        icon: 'none'
      })
      return;
    }
    if(this.data.status==0){
      wx.showToast({
        title: '接龙还未开始呢',
        icon:'none'
      })
      return
    }
    if (this.data.status == 2) {
      wx.showToast({
        title: '接龙已经结束啦',
        icon: 'none'
      })
      return
    }
    var buygood=[];
    if (that.data.isshow==false){
      that.setData({
        isshow:true
      })
      return
    }
    if(this.data.num==0){
      wx.showToast({
        title: '请选择商品',
        icon:'none'
      })
      return
    }
    var tem={
      num:this.data.num,
      unit: this.data.detail.unit,
      optionid: this.data.detail.optionid ? this.data.detail.optionid:'',
      item: this.data.detail.item ? this.data.detail.item : '',
      weight: this.data.detail.weight,
      mprice: this.data.detail.mprice,
      oprice: this.data.detail.oprice,
      id: this.data.detail.id,
      activeid: this.data.detail.activeid,
      gimg: this.data.detail.gimg,
      gname: this.data.detail.gname,
      gprice: this.data.detail.gprice
    }
    buygood.push(tem)
    wx.setStorage({
      key: 'goodsArr',
      data: buygood,
      success:function(res){
        var activeid = wx.getStorageSync('goodsArr')[0].activeid;
        wx.request({
          url: jielong + 'action=checkAction',
          data: {
            uniacid: app.data.GZHid,
            id: that.data.detail.activeid
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            if (res.data.status == 1) {
              wx.navigateTo({
                url: '../creatorder/creatorder',
              })
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: res.data.data,
                complete: function (res) {
                  wx.setStorage({
                    key: 'goodsArr',
                    data: [],
                  })
                  that.setData({
                    num: num
                  })
                }
              })
            }
            that.setData({
              isblock: false
            })
          },
          fail: function (res) {
            that.setData({
              isblock: false
            })
          }
        })
        wx.navigateTo({
          url: '../creatorder/creatorder',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that=this;
    wx.getStorage({
      key: 'noright',
      success: function (res) {
        that.setData({
          noright: res.data
        },()=>{
          wx.hideLoading()
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})